package com.ebelter.temperaturegun.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.app.IConstant;
import com.ebelter.temperaturegun.model.app.TemUtils;

import org.greenrobot.eventbus.EventBus;

public class UnitSwitchActivity extends TBaseActivity {


    public static final String TAG = "UnitSwitchActivity";

    public static boolean isResume = false;
    public static final int CHANGE = 1;
    private ImageView top_iv;
    private TextView top_tv1;
    private TextView top_tv2;
    private ImageView su_du_iv;
    private ImageView su_f_iv;

    @Override
    protected int setContentViewId() {
        return R.layout.activity_unit_switch;
    }

    private int oriUnit;
    private int willUnit;
    @Override
    protected void initView() {
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        top_tv2=findViewById(R.id.top_tv2);
        ViewUtils.displayView(top_tv1);
        ViewUtils.displayView(top_tv2);

        su_du_iv=findViewById(R.id.su_du_iv);
        su_f_iv=findViewById(R.id.su_f_iv);

        top_tv1.setText(R.string.dwqh);
        top_tv2.setText(R.string.baocun);
        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        top_tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
        oriUnit = TemUtils.getUerUnit();
        selectUint(oriUnit);
    }



    private void selectUint(int unit){
        ViewUtils.hideView(su_du_iv);
        ViewUtils.hideView(su_f_iv);
        ViewUtils.displayView(unit==0?su_du_iv:su_f_iv);

    }

    //保存
    private void save() {
        UserSpUtil.writeInt(IConstant.IUserSpCon.UNIT,willUnit);
        CommonEventBus<Integer> commonEventBus = new CommonEventBus(TAG,MainActivity.TAG+SettingActivity.TAG,CHANGE,"单位改变了");
        commonEventBus.setContent(willUnit);//willUnit=0C =1F
        EventBus.getDefault().post(commonEventBus);
        ToastUtil.show(R.string.Set_success);
        finish();
    }


    @Override
    protected void onResume() {
        super.onResume();
        isResume=true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isResume=false;
    }

    //C
    public void du(View view) {
        selectUint(0);
        willUnit = 0;
    }

    //F
    public void f(View view) {
        selectUint(1);
        willUnit = 1;
    }
}
